﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCSY.BLL;
using WCSY.MODEL;
using System.Data.SqlClient;
using System.Data;

public partial class UserManagement_RoleUserList : System.Web.UI.Page
{
    Mg_userManager userM = new Mg_userManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){
            string roleId = Request["roleId"];
            userListBind();
        
        }
    }

    public void userListBind() {
        this.UserListGridView.DataSource = userM.GetAllList();
        this.UserListGridView.DataBind();
    
    }
    protected void handle_Click(object sender, EventArgs e)
    {
        string roleId = Request["roleId"];
        Mg_urRelationManager.DeleteMg_urRelationById(roleId);
        foreach(GridViewRow row in this.UserListGridView.Rows){
            
            if(row.RowType == DataControlRowType.DataRow){
                CheckBox CB = row.FindControl("userSecCheckBox") as CheckBox;

                if(CB.Checked==true){
                    string userId = this.UserListGridView.DataKeys[row.RowIndex]["userId"].ToString();
                    Mg_urRelation ur = new Mg_urRelation();
                    ur.RoleId = roleId;
                    ur.UserId = userId;
                    Mg_urRelationManager.AddMg_urRelation(ur);
     
                }
            
            } 
        }
        Response.Write("<script>window.open('MainRoleUser.aspx?roleId='+'" + roleId + "','User','');window.close();</script>");
       
    }

    protected void UserListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox ck = e.Row.Cells[0].FindControl("userSecCheckBox") as CheckBox;
            string tid = e.Row.Cells[1].Text.Trim();
            string roleId = Request["roleId"];

            if (roleId != null && !roleId.Equals(""))
            {

                DataTable userList = userM.getUserByRole(roleId);
                foreach (DataRow user in userList.Rows)
                {
                    if (user["userId"] != null && user["userId"].ToString().Equals(tid))
                    {
                        ck.Checked = true;
                    }

                }
            };
               
            
        }
    }


    protected void cancel_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close()</script>");
    }
}
